package com.szholly.plug.form.excel.core.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import com.szholly.plug.form.excel.model.TableEntity;

public class ImpBefore extends ImpDataPar implements IImpBefore {

	public ImpBefore(TableEntity tableEntity,String userId,String dsName) {
		super(tableEntity,userId,dsName);
	}
	
	
	@SuppressWarnings("null")
	public void dropTempTable(){
		Connection conn = null;
		Statement stmt = null;
		try {
//			conn = ConnectionHelper.getCurrentContributionConnection(dsName);
			stmt = conn.createStatement();
			StringBuilder sbSqlDropTable = new StringBuilder(
					" drop table ");
			sbSqlDropTable.append(tableName).append(TEMP_TABEL_SUFFIX).append(userId);//临时表加后缀
			stmt.execute(sbSqlDropTable.toString());
		} catch (SQLException e) {
//			e.printStackTrace();
		} finally {
//			DBUtil.close(conn);
//			DBUtil.close(stmt);
		}
	}
	
	
	@SuppressWarnings("null")
	public void createTempTable() {
		Connection conn = null;
		Statement stmt = null;
		try {
//			conn = ConnectionHelper.getCurrentContributionConnection(dsName);
			stmt = conn.createStatement();
			StringBuilder sbSqlCreateTable = new StringBuilder(
					" create table ");
			sbSqlCreateTable.append(tableName).append(TEMP_TABEL_SUFFIX).append(userId);//临时表加后缀
			sbSqlCreateTable.append("(");
			for (int i = 0; i < colLength; i++) {
				if (i < colLength - 1) {
					sbSqlCreateTable.append(arrColumn.get(i) + " varchar(255),");
				}
				if (i == colLength - 1) {
					sbSqlCreateTable.append(arrColumn.get(i) + " varchar(255)");
				}
			}
			sbSqlCreateTable.append(")");
			stmt.execute(sbSqlCreateTable.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
//			DBUtil.close(conn);
//			DBUtil.close(stmt);
		}
	}
	
	
	@SuppressWarnings("null")
	public void insertTempData(List<Map<String, String>> tempData) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
//			conn = ConnectionHelper.getCurrentContributionConnection(dsName);
			int colLength = arrColumn.size();
			StringBuilder sbSqlInsertData = new StringBuilder(" insert into ")
					.append(tableName).append(TEMP_TABEL_SUFFIX).append(userId);
			sbSqlInsertData.append("( ");
			sbSqlInsertData.append(getStrColName());
			sbSqlInsertData.append(") VALUES (");
			sbSqlInsertData.append(getStrParam());
			pstmt = conn.prepareStatement(sbSqlInsertData.toString());
			for (Map<String, String> mapData : tempData) {
				for (int m = 0; m < colLength; m++) {
					pstmt.setString(m + 1, mapData.get(arrColumn.get(m)));
				}
				pstmt.addBatch();
			}
			pstmt.executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
//			DBUtil.close(conn);
//			DBUtil.close(pstmt);
		}
	}
	
	
	@SuppressWarnings("null")
	public void delTempData() {
		Connection conn = null;
		Statement stmt = null;
		try {
//			conn = ConnectionHelper.getCurrentContributionConnection(dsName);
			stmt = conn.createStatement();
			StringBuilder sbSqlDelData = new StringBuilder(
					" delete from ");
			sbSqlDelData.append(tableName).append(TEMP_TABEL_SUFFIX).append(userId);//临时表加后缀
			stmt.execute(sbSqlDelData.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
//			DBUtil.close(conn);
//			DBUtil.close(stmt);
		}
	}
}
